<?php 
class ControllerAccountLogin extends Controller {
	private $error = array();
	
	public function index(){
		
		if($this->customer->isLogged()) $this->redirect($this->url->link('account/account', '', 'SSL'));

        $json=array();
        $json['error']='';
        $json['redirectUrl']='';
								
		if(($this->request->server['REQUEST_METHOD']=='POST') && $this->validate()){
			unset($this->session->data['guest']);
            $json['redirectUrl']=$this->url->link('account/account', '', 'SSL');
        }

		if(isset($this->error['warning'])){
            $this->load->language('account/login');
            $json['error']=$this->language->get('error_login');
        }

        $this->response->setOutput(json_encode($json));
  	}
  
  	private function validate() {
        $email=isset($this->request->post['email']) ? $this->request->post['email'] : '';
        $password=isset($this->request->post['password']) ? $this->request->post['password'] : '';

    	if (!$this->customer->login($email, $password)) {
      		$this->error['warning'] = $this->language->get('error_login');
    	}

        $this->load->model('account/customer');

		$customer_info=$this->model_account_customer->getCustomerByEmail($email);
		
    	if ($customer_info && !$customer_info['approved']) {
      		$this->error['warning'] = $this->language->get('error_approved');
    	}		
		
    	if (!$this->error) {
      		return true;
    	} else {
      		return false;
    	}  	
  	}
}
?>